package com.middle.common.result;

import java.io.Serializable;

@SuppressWarnings("serial")
public class ApiResponse<T> implements Serializable {
	
	
	
	public ApiResponse(){}
	
	/**
	 * 设用是否成功 ，不可缺失
	 */
	private boolean isSuccess;
	/**
	 * 调用code 0000~09999 不可缺失
	 */ 
	private String code;
	
	/**
	 * 数据，可以置空
	 */
	private T data;
	
	/**
	 * 描述信息，可以置空
	 */
	private String message;
	
	@SuppressWarnings("rawtypes")
	public static final  ApiResponse SUCCESS = new ApiResponse(true,"0000","成功");
	
	@SuppressWarnings("rawtypes")
	public static final  ApiResponse LOGIN_USERPASSWORD_ERROR = FailCustomWithMessage("0001","账号或密码错误！");
	
	@SuppressWarnings("rawtypes")
	public static final ApiResponse UNKNOWN_ERROR = FailCustomWithMessage("0002","未知异常！"); 
	
	@SuppressWarnings("rawtypes")
	public static final  ApiResponse FAIL_WITH_PARAMS_ERROR = FailCustomWithMessage("0003","参数异常！");
	
	@SuppressWarnings("rawtypes")
	public static final  ApiResponse FAIL_WITH_PARAME_MISSING = FailCustomWithMessage("0004","参数缺失！"); 
	
	@SuppressWarnings("rawtypes")
	public static final  ApiResponse FAIL_WITH_PARAME_FORMAT_ERROR = FailCustomWithMessage("0005","数据类型转换异常！"); 
	
	@SuppressWarnings("rawtypes")
	public static final  ApiResponse FAIL_WITH_NOT_LOGIN_ERROR = FailCustomWithMessage("0006","未登陆系统！"); 

	/**
	 * 成功带上参数
	 * @param <T>
	 * @param data
	 * @return
	 */
	public static <T> ApiResponse<T> successWithData(T data) {
		return new ApiResponse<>(true,"0000",data,"成功");
	}
	
	/**
	 * 失败，自定义异常信息
	 * @param <T>
	 * @param code
	 * @param message
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static ApiResponse FailCustomWithMessage(String code,String message) {
		return new ApiResponse<>(false,code,null,message);
	}
	
	public ApiResponse(boolean isSuccess,String code,T data,String message){
		this.isSuccess = isSuccess;
		this.code = code;
		this.data = data;
		this.message = message;
	}
	
	public ApiResponse(boolean isSuccess,String code) {
		this(isSuccess,code,null,null);
	}
	
	public ApiResponse(boolean isSuccess,String code,T data) {
		this(isSuccess,code,data,null);
	}
	
	public ApiResponse(boolean isSuccess,String code,String message) {
		this(isSuccess,code,null,message);
	}
	
	public boolean isSuccess() {
		return isSuccess;
	}


	public String getCode() {
		return code;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	
}
